import { faker } from '@faker-js/faker';

// 生成 100 条随机用户数据
const users = Array.from({ length: 100 }, () => ({
  id: faker.string.uuid(),
  nickname: faker.person.firstName(),
  accessor: faker.internet.username(),
  email: faker.internet.email(),
  phone: faker.phone.number(),
  avatar: faker.image.avatar(),
  role: faker.helpers.arrayElement(['admin', 'user', 'guest']),
  createdAt: faker.date.past(),
  status: faker.helpers.arrayElement([0, 1]),
  create_time: faker.date.past(),
  update_time: faker.date.past(),
  token: faker.string.uuid(),
  password: faker.internet.password(),
}));

const UserList = [
  {
    id: '00000001',
    accessor: 'admin',
    nickname: 'John Brown',
    avatar: 'https://joeschmoe.io/api/v1/random',
    email: 'admin@ant.design',
    phone: '1234567890',
    status: 1,
    create_time: '2023-10-01 12:00:00',
    update_time: '2023-10-01 12:00:00',
    remark: '这是一个备注',
    token: '1234567890',
    password: '123456',
  },
  {
    id: '00000002',
    phone: '1234567890',
    nickname: 'Jim Green',
    accessor: 'user',
    email: 'user@ant.design',
    avatar: 'https://joeschmoe.io/api/v1/random',
    status: 0,
    create_time: '2023-10-01 12:00:00',
    update_time: '2023-10-01 12:00:00',
    remark: '这是一个备注',
    toekn: '1234567890',
    password: '123456',
  },
  {
    id: '00000003',
    phone: '1234567890',
    nickname: 'Joe Black',
    accessor: 'guest',
  },
  ...users,
];
export default [
  {
    url: '/api/user',
    method: 'get',
    response: (query) => {
      const { page = 1, pageSize = 10 } = query.query; // 获取分页参数
      const start = (page - 1) * pageSize;
      const end = start * 1 + pageSize * 1;
      console.log(start, 'start', end);
      const paginatedList = UserList.slice(start, end); // 分页处理

      return {
        code: 200,
        data: {
          list: paginatedList,
          total: UserList.length,
        },
      };
    },
  },
  {
    url: '/api/user',
    method: 'post',
    response: (query) => {
      const body = query.body;
      console.log(body, 'add-body');
      UserList.push({
        id: UserList.length + 1,
        ...body,
      });
      return {
        data: {
          code: 200,
          data: `添加成功`,
        },
      };
    },
  },
  {
    url: '/api/user',
    method: 'put',
    response: (query) => {
      const body = query.body;
      UserList.push({
        id: UserList.length + 1,
        ...body,
      });
      return {
        code: 200,
        data: `修改成功`,
      };
    },
  },
  {
    url: '/api/user',
    method: 'delete',
    response: (query) => {
      const body = query.body;
      const index = UserList.findIndex((item) => item.id === body.id);
      UserList.splice(index, 1);
      return {
        code: 200,
        data: `删除成功`,
      };
    },
  },
  // 登录
  {
    url: '/api/login',
    method: 'post',
    response: (query) => {
      console.log(query, 'query--前端传入参数');
      const body = query.body; // 获取请求体中的用户名和密码
      const obj = UserList.find(
        (item) =>
          item.accessor === body.accessor && item.password === body.password,
      );
      if (!obj) {
        return {
          code: 400,
          data: {
            msg: '用户名或密码错误',
          },
        };
      }
      return {
        code: 200,
        data: {
          token: '1234567890',
          user: obj,
          msg: '登录成功',
        },
      };
    },
  },
]; //
